//
//  HYAudioPlayAreaView.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/22.
//

#import "HYAudioPlayAreaView.h"
#import <HYVideoPlayer/HYVideoPlayerConfigure.h>
#import <HYUIKit/HYAppleLoadingView.h>
#import "UIImageView+PlayGIF.h"

#define kBtnWidth (kIsIPad?40:30)

@interface HYAudioPlayAreaView ()

@property (nonatomic, strong) UIImageView *logoImage;

@property (nonatomic, strong) HYAudioPlayerProgressView *progressView;

@end

@implementation HYAudioPlayAreaView

- (void)dealloc
{
    CLog(@"dealloc -- %@",self.class);
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        CGFloat uiscale = UI_SCALE;
        CGFloat gapscale = GAP_SCALE;
        if(gapscale > 1.0 && gapscale < 1.28){// 为适配6 7 8 plus
            gapscale = 1.27;
        }
        CGFloat margin = kIsIPad?65:40;
        CGFloat llrWidth = 78*gapscale;
        //380*393
        CGFloat liWidth = (kIsIPad?300:250)*uiscale;
        NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"audio_play" ofType:@".gif"];
        UIImageView *logoImage = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:imagePath]];
        logoImage.gifPath = imagePath;
        [self addSubview:logoImage];
        [logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.width.height.mas_equalTo(liWidth);
        }];
        self.logoImage = logoImage;
        
        CGFloat tltWidth = 27*gapscale;
        UILabel *titleLabel = [UILabel new];
        [self addSubview:titleLabel];
        titleLabel.textColor = APPTextThemeColor;
        titleLabel.font = [UIFont systemFontOfSize:(MIDDLE_FONT_SIZE)*uiscale];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.top.mas_equalTo(logoImage.mas_bottom).mas_equalTo(tltWidth);
        }];
        titleLabel.numberOfLines = 2;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel = titleLabel;
        
        UILabel *authorLabel = [UILabel new];
        [self addSubview:authorLabel];
        authorLabel.textColor = [CommUtls colorWithHexString:@"#999999"];
        authorLabel.font = [UIFont systemFontOfSize:MIN_FONT_SIZE];
        [authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(titleLabel.mas_bottom).mas_equalTo(11);
        }];
        self.authorLabel = authorLabel;

        HYAudioPlayerProgressView *progressView = [[HYAudioPlayerProgressView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, BOTTOM_SLIDER_HEIGHT)];
        [self addSubview:progressView];
        [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(BOTTOM_SLIDER_HEIGHT);
            make.top.mas_equalTo(titleLabel.mas_top).mas_equalTo(83*gapscale);
        }];
        self.progressView = progressView;
        
        CGFloat pbtGap = 37*gapscale;
        UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:playButton];
        [playButton setImage:[UIImage imageNamed:@"icon_yp_play"] forState:UIControlStateNormal];
        [playButton setImage:[UIImage imageNamed:@"icon_yp_pause"] forState:UIControlStateSelected];
        [playButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.width.height.mas_equalTo(kIsIPad?60:50);
            make.top.mas_equalTo(progressView.mas_bottom).mas_equalTo(pbtGap);
        }];
        self.playButton = playButton;
        
        UIButton *lastButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:lastButton];
        [lastButton setImage:[UIImage imageNamed:@"icon_yp_last"] forState:UIControlStateNormal];
        [lastButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(playButton.mas_left).mas_equalTo(-margin*gapscale);
            make.width.height.mas_equalTo(kIsIPad?40:25);
            make.centerY.mas_equalTo(playButton);
        }];
        self.lastButton = lastButton;
        
        UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:nextButton];
        [nextButton setImage:[UIImage imageNamed:@"icon_yp_next"] forState:UIControlStateNormal];
        [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(playButton.mas_right).mas_equalTo(margin*gapscale);
            make.width.height.mas_equalTo(kIsIPad?40:25);
            make.centerY.mas_equalTo(playButton);
        }];
        self.nextButton = nextButton;
        
        UIButton *ktButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:ktButton];
        [ktButton setImage:[UIImage imageNamed:@"icon_yp_kt"] forState:UIControlStateNormal];
        [ktButton mas_makeConstraints:^(MASConstraintMaker *make) {
            if(kIsIPad){
                make.left.mas_equalTo(llrWidth);
            }else{
                make.centerX.equalTo(lastButton.mas_left).mas_equalTo(-margin*gapscale);
            }
            make.width.height.mas_equalTo(kBtnWidth);
            make.centerY.mas_equalTo(playButton);
        }];
        @weakify(self);
        [[ktButton rac_signalForControlEvents:UIControlEventTouchUpInside]
         subscribeNext:^(id x) {
             @strongify(self);
             [self rotating:self.ktButton clockwise:NO];
         }];
        self.ktButton = ktButton;
        
        UIButton *kjButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:kjButton];
        [kjButton setImage:[UIImage imageNamed:@"icon_yp_kj"] forState:UIControlStateNormal];
        [kjButton mas_makeConstraints:^(MASConstraintMaker *make) {
            if(kIsIPad){
                make.right.mas_equalTo(-llrWidth);
            }else{
                make.centerX.mas_equalTo(nextButton.mas_right).mas_equalTo(margin*gapscale);
            }
            make.width.height.mas_equalTo(kBtnWidth);
            make.centerY.mas_equalTo(playButton);
        }];
        [[kjButton rac_signalForControlEvents:UIControlEventTouchUpInside]
         subscribeNext:^(id x) {
             @strongify(self);
             [self rotating:self.kjButton clockwise:YES];
         }];
        self.kjButton = kjButton;
        
        UIButton *playModeButton = [[UIButton alloc]init];
        [playModeButton setImage:[UIImage imageNamed:@"icon_yp_sx"] forState:UIControlStateNormal];
        [self addSubview:playModeButton];
        [playModeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(ktButton.mas_centerX);
            make.width.height.mas_equalTo(kBtnWidth);
            make.top.equalTo(ktButton.mas_bottom).offset((kIsIPad?135:(iPhoneX?90:80))*gapscale);
            make.bottom.mas_equalTo(0);
        }];
        self.playModeButton = playModeButton;
        
        UIImageView *speedView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_yp_speed"]];
        [self addSubview:speedView];
        [speedView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(playButton.mas_centerX);
            make.width.height.mas_equalTo(kBtnWidth);
            make.centerY.equalTo(playModeButton.mas_centerY);
        }];
        
        UIButton *speedButton = [[UIButton alloc]init];
        speedButton.titleLabel.font = FONT_SIZE(14);
        [speedButton setTitleColor:APPTextThemeColor forState:UIControlStateNormal];
        [self addSubview:speedButton];
        [speedButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(playButton.mas_centerX);
            make.width.height.mas_equalTo(40);
            make.centerY.equalTo(playModeButton.mas_centerY);
        }];
        self.speedButton = speedButton;
        
        UIButton *collectButton = [[UIButton alloc]init];
        [collectButton setImage:[UIImage imageNamed:@"icon_yp_uncollect"] forState:UIControlStateNormal];
        [collectButton setImage:[UIImage imageNamed:@"icon_yp_collect"] forState:UIControlStateSelected];
        [self addSubview:collectButton];
        [collectButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(lastButton.mas_centerX);
            make.width.height.mas_equalTo(kBtnWidth);
            make.centerY.equalTo(playModeButton.mas_centerY);
        }];
        self.collectButton = collectButton;
        
        UIButton *downloadButton = [[UIButton alloc]init];
        [downloadButton setImage:[UIImage imageNamed:@"icon_yp_undl"] forState:UIControlStateNormal];
        [downloadButton setImage:[UIImage imageNamed:@"icon_yp_dl"] forState:UIControlStateSelected];
        [self addSubview:downloadButton];
        [downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(nextButton.mas_centerX);
            make.width.height.mas_equalTo(kBtnWidth);
            make.centerY.equalTo(playModeButton.mas_centerY);
        }];
        self.downloadButton = downloadButton;
        
        UIButton *catalogButton = [[UIButton alloc]init];
        [catalogButton setImage:[UIImage imageNamed:@"icon_yp_ml"] forState:UIControlStateNormal];
        [self addSubview:catalogButton];
        [catalogButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(kjButton.mas_centerX);
            make.width.height.mas_equalTo(kBtnWidth);
            make.centerY.equalTo(playModeButton.mas_centerY);
        }];
        self.catalogButton = catalogButton;
    }
    return self;
}

- (void)rotating:(UIView *)view clockwise:(BOOL)clockwise 
{
    [view.layer removeAllAnimations];
    
    CABasicAnimation * rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //让其在z轴旋转
    rotationAnimation.toValue = clockwise?@(M_PI*2.0):@(-M_PI*2.0);//旋转角度
    rotationAnimation.duration = .7; //旋转周期
    rotationAnimation.cumulative = YES;//旋转累加角度
    rotationAnimation.repeatCount = 1;//旋转次数
    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

- (void)setPlaying:(BOOL)playing 
{
    if (!self.logoImage.hidden) {
        if(playing){
            [self.logoImage startGIF];
        }else{
            [self.logoImage stopGIF];
        }
    }
}

- (void)setAreaResponseState:(BOOL)state 
{
    self.progressView.userInteractionEnabled = state;
    self.kjButton.userInteractionEnabled = state;
    self.ktButton.userInteractionEnabled = state;
    self.playButton.userInteractionEnabled = state;
    self.catalogButton.userInteractionEnabled = state;
}

- (void)setAudioTitle:(NSString *)audioTitle
{
    _audioTitle = audioTitle;
    NSArray *titles;
    if([audioTitle componentsSeparatedByString:@" - "]){
        titles = [audioTitle componentsSeparatedByString:@" - "];
    }else if ([audioTitle componentsSeparatedByString:@"-"]){
        titles = [audioTitle componentsSeparatedByString:@"-"];
    }
     
    if(titles.count>1){
        NSString *author = titles.firstObject;
        self.titleLabel.text = titles[1];
        self.authorLabel.text = author;
        self.authorLabel.hidden = NO;
    }else{
        self.titleLabel.text = audioTitle;
        self.authorLabel.hidden = YES;
    }
}

@end
